package logic

import (
	"context"
	"fmt"
	"hui-hui/hui_hui_rpc/huihuirpcclient"
	"hui-hui/models"

	"hui-hui/hui_hui_rpc/hui_hui_rpc"
	"hui-hui/hui_hui_rpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type ListResaLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewListResaLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ListResaLogic {
	return &ListResaLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *ListResaLogic) ListResa(in *hui_hui_rpc.ListResaRequest) (*hui_hui_rpc.ListResaResponse, error) {
	// todo: add your logic here and delete this line
	var resas []struct {
		models.Resa
		UserName string
	}
	var data []*hui_hui_rpc.FromListResa
	if models.DB.Model(&models.Resa{}).Joins("join users on resas.user_id = users.id").Where("resas.plan_id = ?", in.PlanId).Select("resas.*,users.name as user_name").Scan(&resas).Error != nil {
		return nil, fmt.Errorf("服务器繁忙")
	}
	for _, resa := range resas {
		get := huihuirpcclient.FromListResa{
			UserName:  resa.UserName,
			CreatedAt: resa.CreatedAt.String(),
			Comment:   resa.Comment,
		}
		data = append(data, &get)
	}
	return &hui_hui_rpc.ListResaResponse{
		ResaList: data,
	}, nil
}
